//
//  CXHTopOnInterstitialEvent.m
//  CXHAdSDK-TopOn
//
//  Created by 陈坤 on 2024/10/11.
//

#import "CXHTopOnInterstitialEvent.h"
#import "CXHTopOnCommonBiddingManager.h"
#import "CXHTopOnCommonBiddingRequest.h"

@interface CXHTopOnInterstitialEvent ()
@property (nonatomic, assign) BOOL isReady;
@property (nonatomic, strong) NSString *biddingPrice;
@end

@implementation CXHTopOnInterstitialEvent

/**
 CXHAdSDKIntertitialAd请求成功回调
 
 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdSuccedToLoad:(CXHAdSDKInterstitialAd *)interstitialAd{
    self.isReady = YES;
    if (self.isC2SBiding) {
        CXHTopOnCommonBiddingRequest *request = [[CXHTopOnCommonBiddingManager sharedInstance] getRequestItemWithUnitID:self.UUID];
        ATBidInfo *bidInfo = [ATBidInfo bidInfoC2SWithPlacementID:request.placementID
                                                  unitGroupUnitID:request.unitGroup.unitID
                                               adapterClassString:request.unitGroup.adapterClassString
                                                            price:@(interstitialAd.getBidPrice/100.0f).stringValue
                                                     currencyType:ATBiddingCurrencyTypeCNY
                                               expirationInterval:request.unitGroup.bidTokenTime
                                                     customObject:interstitialAd];
        bidInfo.networkFirmID = request.unitGroup.networkFirmID;
        
        if (request.bidCompletion) {
            request.bidCompletion(bidInfo, nil);
        }
        self.isC2SBiding = NO;
    } else {
        [self trackInterstitialAdLoaded:interstitialAd adExtra:nil];
    }
}

/**
 CXHAdSDKIntertitialAd请求失败回调

 @param interstitialAd 插屏广告实例对象
 @param error 失败原因
*/
- (void)CXH_interstitialAdFailedToLoad:(CXHAdSDKInterstitialAd *)interstitialAd error:(NSError *)error{
    if (self.isC2SBiding) {
        CXHTopOnCommonBiddingRequest *request = [[CXHTopOnCommonBiddingManager sharedInstance] getRequestItemWithUnitID:self.UUID];
        if (request.bidCompletion) {
            request.bidCompletion(nil, error);
        }
        [[CXHTopOnCommonBiddingManager sharedInstance] removeRequestItemWithUnitID:self.UUID];
    } else {
        [self trackInterstitialAdLoadFailed:error];
    }
}

/**
 CXHAdSDKIntertitialAd展示在屏幕内回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdDidPresent:(CXHAdSDKInterstitialAd *)interstitialAd{
    
}

/**
 CXHAdSDKIntertitialAd展示在屏幕内失败回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdFailedToPresent:(CXHAdSDKInterstitialAd *)interstitialAd error:(NSError *)error{
    [self trackInterstitialAdShowFailed:error];
}

/**
 CXHAdSDKIntertitialAd点击回调

 @param interstitialAd 插屏广告实例对象
 @param loadingPageURL 广告落地页地址，当渠道为bwt，并且customLoadingPage为YES时有值
*/
- (void)CXH_interstitialAdDidClick:(CXHAdSDKInterstitialAd *)interstitialAd loadingPageURL:(NSString *)loadingPageURL{
    [self trackInterstitialAdClick];
}

/**
 CXHAdSDKIntertitialAd关闭回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdDidClose:(CXHAdSDKInterstitialAd *)interstitialAd{
    [self trackInterstitialAdClose:@{}];
}

/**
 CXHAdSDKIntertitialAd展示回调
 
 @param interstitialAd 广告实例
 */
- (void)CXH_interstitialAdExposure:(CXHAdSDKInterstitialAd *)interstitialAd{
    [self trackInterstitialAdShow];
}

/**
 CXHAdSDKIntertitialAd展示上报回调
 
 @param interstitialAd 广告实例
 */
- (void)CXH_interstitialAdExposureReport:(CXHAdSDKInterstitialAd *)interstitialAd{
    
}


/**
 CXHAdSDKIntertitialAd点击上报回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdDidClickReport:(CXHAdSDKInterstitialAd *)interstitialAd{
    
}

/**
 CXHAdSDKIntertitialAd关闭落地页回调
 
 @param interstitialAd 广告实例
 */
- (void)CXH_interstitialAdCloseLandingPage:(CXHAdSDKInterstitialAd *)interstitialAd{
    [self trackInterstitialAdLPClose:@{}];
}

- (void)dealloc {
    NSLog(@"------> %s", __FUNCTION__);
}

@end
